package com.cee.service.Impl;

import com.cee.dao.OneSelectMapper;
import com.cee.dto.OneSelectDTO;
import com.cee.model.Exam;
import com.cee.model.OneSelect;
import com.cee.model.OptionOneSelect;
import com.cee.service.OneSelectService;
import com.cee.service.OptionOneSelectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
@Transactional
public class OneSelectServiceImpl implements OneSelectService {

    @Autowired
    private OneSelectMapper oneSelectMapper;

    @Autowired
    private OptionOneSelectService optionOneSelectService;

    @Override
    public int saveOneSelect(OneSelect oneSelect) throws Exception {
        return oneSelectMapper.insert(oneSelect);
    }

    @Override
    public List<OneSelectDTO> getOneSelectList(OneSelectDTO oneSelectDTO) {

        List<OneSelectDTO> responseList = new ArrayList<>();

        List<OneSelectDTO> list = oneSelectMapper.getOneSelectList(oneSelectDTO);

        if (list != null){
            for (OneSelectDTO one : list){
                Integer id = one.getId();
                List<OptionOneSelect> olist = optionOneSelectService.getListById(id);
                one.setList(olist);

                responseList.add(one);
            }
        }
        return responseList;
    }

    @Override
    public List<OneSelectDTO> getListByExamId(Integer examId) {
        return oneSelectMapper.getListByExamId(examId);
    }


    @Override
    public void deleteOneSelect(Integer id) {
        OneSelect oneSelect = new OneSelect();
        oneSelect.setId(id);
        oneSelect.setDelDateTime(new Date());

        log.info("逻辑删除oneSelect, id={}", id);
        oneSelectMapper.updateOneSelectById(oneSelect);
    }
}
